package com.nuorui.module.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuorui.common.constant.CommonConstants;
import org.springframework.stereotype.Service;
import com.nuorui.module.system.mapper.DictMapper;
import com.nuorui.module.system.service.IDictService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 字典表
 *
 * @author
 * @date 2021-12-15 14:10:11
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements IDictService {


    @Override
    public Map<String, List<SysDict>> selectDictMap() {
        Map<String, List<SysDict>> recordMap = new HashMap<>();

        List<SysDict> dataList = baseMapper
                .selectList(Wrappers.<SysDict>lambdaQuery()
                        .gt(SysDict::getParentId, CommonConstants.DICT_ROOT_ID)
                        .orderByDesc(SysDict::getOrderNum)
                );

        if (CollUtil.isNotEmpty(dataList)) {
            for (SysDict record : dataList) {
                List<SysDict> recordList = new ArrayList<>();
                if (recordMap.containsKey(record.getDictCode())) {
                    recordList = recordMap.get(record.getDictCode());
                }
                recordList.add(record);
                recordMap.put(record.getDictCode(), recordList);
            }
        }
        return recordMap;
    }
}
